/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.plugin;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import kameleon.exception.NotUniquePlugInInfoException;
import kameleon.exception.UnknownExtensionException;

/**
 * Utility class for the mapping between the file extensions of the analyzed files 
 * and the analyzer information.
 * 
 * @author		Schnell Michaël
 * @version		1.0
 */
public class AnalyzerManager {

	/**
	 * Mapping between the extensions and the analyzer informations. The keys 
	 * for the hashtable are the extensions, each extension is mapped to a list 
	 * of analyzer informations known to use the given extension.
	 */
	protected Hashtable<String, List<PlugInInfo>> analyzerInfo ;
	
	/**
	 * Sole constructor.
	 */
	public AnalyzerManager() {
		super() ;
		this.analyzerInfo = new Hashtable<String, List<PlugInInfo>>() ;
	}// AnalyzerManager()
	
	/**
	 * Determines if the given extension is known by this manager. An extension is known if there
	 * is at least one analyzer attached to it.
	 * 
	 * @param 	extension
	 * 			searched extension
	 * @return	{@code true} if the extension is known, {@code false} otherwise
	 */
	public boolean isKnownExtension(String extension) {
		return (extension != null) && this.analyzerInfo.containsKey(extension) ;
	}// isKnownExtension(String)
	
	/**
	 * Returns the number of analyzers known to use the given extension.
	 * 
	 * @param 	extension
	 * 			extension for which the number of analyzers if searched
	 * 
	 * @return	Number of analyzers known to use the given extension
	 * 
	 * @throws 	UnknownExtensionException
	 * 			if the extension is unknown
	 * 
	 * @see		#isKnownExtension(String)
	 */
	public int getNAnalyzers(String extension) throws UnknownExtensionException {
		if (!this.isKnownExtension(extension)) {
			throw new UnknownExtensionException() ;
		}// if
		
		return this.analyzerInfo.get(extension).size() ;
	}// getNAnalyzers(String)
	
	/**
	 * Adds extension-analyzer mapping for the given analyzer to this manager.
	 * 
	 * @param	info
	 * 			information about the new analyzer
	 */
	public void addAnalyzerInfo(PlugInInfo info) {
		for(String extension : info.getExtensions()) {
			if (this.isKnownExtension(extension)) {
				this.analyzerInfo.get(extension).add(info) ;
			} else {
				List<PlugInInfo> list = new ArrayList<PlugInInfo>() ;
				list.add(info) ;
				this.analyzerInfo.put(extension, list) ;
			}// if
		}// for
	}//addAnalyzerInfo(PlugInInfo)
	
	/**
	 * Removes extension-analyzer mapping for the given analyzer from this manager.
	 * 
	 * @param	info
	 * 			information about the deleted analyzer
	 */
	public void removeAnalyzerInfo(PlugInInfo info) {
		for(String extension : info.getExtensions()) {
			if (this.isKnownExtension(extension)) {
				List<PlugInInfo> list = this.analyzerInfo.get(extension) ;
				list.remove(info) ;
				/* If there are no plug-ins left for an extension, 
				 * we remove it from the hashtable. */
				if (list.isEmpty()) {
					this.analyzerInfo.remove(extension) ;
				}// if
			}// if
		}// for
	}// removeAnalyzerInfo(PlugInInfo)
	
	/**
	 * Returns the informations about the single analyzer known to use this extension.
	 * 
	 * @param	extension
	 * 			extension for which the analyzer is requested
	 * 
	 * @return	Informations about the single analyzer known to use this extension
	 * 
	 * @throws	NotUniquePlugInInfoException
	 * 			if there are more than one analyzer using the given extension
	 * 
	 * @throws	UnknownExtensionException
	 * 			if there is no analyzer using this extension
	 * 
	 * @see		#isKnownExtension(String)
	 */
	public PlugInInfo getAnalyzerInfo(String extension) throws NotUniquePlugInInfoException, UnknownExtensionException {
		if (!this.isKnownExtension(extension)) {
			throw new UnknownExtensionException() ;
		}// if
		
		if (this.getNAnalyzers(extension) != 1) {
			throw new NotUniquePlugInInfoException() ;
		}// if
		
		return this.analyzerInfo.get(extension).get(0) ;
	}// getAnalyzerInfo(String)
	
	/**
	 * Returns the informations about all the analyzers known to use this extension.
	 * 
	 * @param	extension	
	 * 			extension for which the analyzers are requested
	 * 
	 * @return	Informations about all the analyzers known to use this extension
	 * 
	 * @throws	UnknownExtensionException	
	 * 			if there is no analyzer using this extension
	 */
	public List<PlugInInfo> getAllAnalyzerInfo(String extension) throws UnknownExtensionException {
		if (!this.isKnownExtension(extension)) {
			throw new UnknownExtensionException() ;
		}// if
		
		return this.analyzerInfo.get(extension) ;
	}// getAllAnalyzerInfo(String)
	
}// class AnalyzerManager